<template>
  <div>
    <el-tree
      ref="refTree"
      :data="list"
      show-checkbox
      check-strictly
      default-expand-all
      :props="{label:'name'}"
      node-key="id"
    />

    <el-button size="small" @click="hClose">取消</el-button>
    <el-button size="small" type="primary" @click="hSubmit">确定</el-button>

  </div>
</template>

<script>

import { getPermissionList } from '@/api/permissions'
import { tranListToTreeData } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },

  created() {
    this.loadPermissionList()
    this.loadRoleDetail()
  },
  methods: {
    hClose() {
      this.$emit('close')
    },
    async hSubmit() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      const res = await assignPerm({ id: this.roleId, permIds })
      console.log('保存权限', res)
      this.$message.success('修改权限成功')
      this.$emit('close')
    },

    // 获取现有权限
    async loadRoleDetail() {
      const res = await getRoleDetail(this.roleId)
      //   console.log('获得现有权限', res.data.permIds)
      this.$refs.refTree.setCheckedKeys(res.data.permIds)// 回填   绑定自身原有权限
    },

    // 获取所有权限
    async loadPermissionList() {
      const res = await getPermissionList()
      //   console.log('获得所有权限', res)
      this.list = tranListToTreeData(res.data)
    //   console.log('转换树形', list)
    }
  }
}
</script>

<style>

</style>
